package com.nit.bike.controller.basic;

import com.alibaba.fastjson.JSONObject;
import com.nit.bike.dto.basic.UserDTO;
import com.nit.bike.dto.system.TokenDTO;
import com.nit.bike.dto.utils.OpResultDTO;
import com.nit.bike.dto.utils.TableReqDTO;
import com.nit.bike.dto.utils.TableRspDTO;
import com.nit.bike.service.basic.UserService;
import com.nit.bike.utils.AuthorizationUtil;
import com.nit.bike.utils.HttpRequestReader;
import com.nit.bike.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/user")
public class UserController {

    @Resource
    private UserService userService;
    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * 获取数据列表
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO req) {
        TableRspDTO rsp = new TableRspDTO();
        try {
            if (AuthorizationUtil.adminInterfaceBool(token, rsp)) {
                rsp = userService.list4Table(req);
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return rsp;
    }

    /**
     * 新增记录
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody UserDTO userDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            op.setMessage(userService.add(userDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 更新用户基本信息
     */
    @RequestMapping(value = "/updateBasicInfo", method = RequestMethod.POST)
    public OpResultDTO updateBasicInfo(@RequestHeader(name = "Access-Token") String token, @RequestBody UserDTO userDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            userDTO.setUserId(JWTUtil.verifyToken(token).getAccountId());
            op.setResult(userService.updateBasicInfo(userDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 更新记录
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestHeader(name = "Access-Token") String token, @RequestBody UserDTO userDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminAndSelfBool(token, userDTO.getUserId(), op)) {
                op.setResult(userService.update(userDTO).toString());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 修改用户状态
     */
    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    public OpResultDTO updateState(@RequestHeader(name = "Access-Token") String token, HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminInterfaceBool(token, op)) {
                JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
                String ids = jsonRequest.getString("ids");
                Integer state = jsonRequest.getInteger("state");
                op.setResult(userService.updateState(ids, state).toString());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 重置密码
     */
    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
    public OpResultDTO resetPwd(@RequestHeader(name = "Access-Token") String token, @RequestBody UserDTO userDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminAndSelfBool(token, userDTO.getUserId(), op)) {
                op.setMessage(userService.resetPwd(userDTO.getUserId(), userDTO.getLoginPwd()).toString());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 登录账号重名验证
     */
    @RequestMapping(value = "/validateLoginName", method = RequestMethod.POST)
    public OpResultDTO validateLoginName(HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String loginName = jsonRequest.getString("loginName");
            if (userService.loginCheck(loginName) == null) {
                op.setMessage("success");
            } else {
                op.setMessage("error");
            }
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 根据Id获取信息
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public OpResultDTO getInfo(@RequestHeader(name = "Access-Token") String token) {
        OpResultDTO op = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            op.setMessage("success");
            op.setResult(userService.getByUserId(tokenDTO.getAccountId()));
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }
}
